
// 封装一个方法去掉指定符号得到数组 (不能使用数组方法)
// 如场景 把字符串'aa&&bb&&cc&&dd'，去掉字符&& 转成数组

// let str = 'aa&&bb&&cc&&dd';

let str = 'aa&&&bb&&cc&&dd';
// let str = 'aa&&bab&&cc&&&dd';
// let str = 'aa&&bb&&&cc&&&dd&';
// let str = 'aa&&&bb&&cc&&dd&';

const tag = '&&';

function strToArr(str, tag) {
  const reg = new RegExp(`${tag}`, 'g');
  str = str.replace(reg, '#');

  const reg_tag = /(.+?)#/g;

  let lastStr = str.slice(str.lastIndexOf('#') + 1);

  let newArr = [];

  str.replace(reg_tag, (node, key) => newArr.push(key));

  newArr.push(lastStr);
  
  console.log(newArr)
  return newArr
}

strToArr(str, tag);


function strToArrSimple(str, tag) {
  const reg_tag = new RegExp(`(.+?)(${tag})?(.+?)`, 'g');

  let newArr = str.match(reg_tag).filter(item => item !== tag);

  return newArr;
}
// console.log(strToArrSimple(str, tag));


let fn = (str) => {
  console.log(str.split('&&'))

  str = str.replace(/\&\&/g, '#') 
  console.log(str);
  let res = []
  let i = -1, j = 0
  // 拓展字符串两边 起始-1，末尾length+1
  // 默认起始，和末尾都为#
  // i，j分别标记相邻两#
  while (i <= str.length && j <= str.length) {
      // 用j找后边的#
      if (str[j] == '#' || j == str.length) {
          // 找到后 截取i j
          const temp = j > i ? str.slice(i + 1, j) : ''
          res.push(temp)
          // i移到后面#的位置, j继续后走
          i = j
          j++
      } else {
          j++
      }
  }
  console.log(res);
}


let list = [
  'aa&&&bb&&cc&&dd',
  'aa&&bab&&cc&&&dd',
  'aa&&bb&&&cc&&&dd&',
  'aa&&bb&&&cc&&dd&',
  'aa&&&bb&&cc&&dd&',
]
// fn(list[Math.ceil(Math.random() * list.length - 1)])